package bruteForce;

public class KnapsackProblem {
    private int[] values;
    private int[] weights;

    public KnapsackProblem(int[] values, int[] weights) {
        this.values = values;
        this.weights = weights;
    }

    public int maxValue(int goalWeight) {
        if (goalWeight == 0 || values.length != weights.length || values.length == 0)
            return 0;
        int maxValue = 0;
        int tempValue = 0;
        int tempWeight = 0;
        for (int i = 0; i < values.length; i++) {
            tempValue = values[i];
            tempWeight = weights[i];
            if (tempWeight <= goalWeight) {
                if (tempValue > maxValue)
                    maxValue += tempValue;
            } else continue;
            for (int j = i + 1; j < values.length; j++) {
                tempValue += values[j];
                tempWeight += weights[j];
                if (tempWeight <= goalWeight) {
                    if (tempValue > maxValue)
                        maxValue = tempValue;
                }
            }
        }
        return maxValue;
    }
}
